var uploadPage;
$(function () {
    uploadPage = function (count) {
    	this.maxImageCount = count;
        this.init();
    };

    uploadPage.prototype = {
      init: function () {
          this.initHtml();
          this.bindEvent();
      },
      initHtml: function () {
          var html = [];
          html.push('<div id="uploadMask" style="display:none;" class="dingwei">');
          html.push('</div>');
          html.push('<div id="innerMask" style="z-index:10;display:none;" class="sctxbg">');
          html.push('<div class="paizhaobg">');
          html.push('<p id="uploadFromCamera" class="mbbottom paizhao">拍照</p>');
          html.push('<p id="uploadFromAlbums" class="paizhao">从相册中选择</p>');
          html.push('</div>');
          html.push('<div class="paizhaobg mtop10">');
          html.push('<p id="cloaseUploadMask" class="paizhao">取消</p>');
          html.push('</div>');
          html.push('</div>');
          
          $("#main").append(html.join(""));
      },
      bindEvent: function () {
        //取消
        $("#cloaseUploadMask").on("click", function (e) {
            $("#main #uploadMask").hide();
            $("#main #innerMask").hide();
        });

      },
      show: function () {
        $("#main #uploadMask").show();
        $("#main #innerMask").show();
      },
      hide: function () {
        $("#main #uploadMask").hide();
        $("#main #innerMask").hide();
      },
      bindUploadShows:function(){

        var model = this;
        if(AGENT_CLIENT.APP){
          //点击添加弹出蒙板
          $("#main").on("click", "span[name='selectUploadType']", function(){
//        if(counts>0 && $("img[name='addImage']").length>=counts){
//          $.toast("只能上传" + counts + "张图片");
//          return;
//        }
					
          model.show();
          });
        }else{
          //选择图片
          $("#main").on("change","input[name='addImgFile']", function () {
//          if(counts>0 && $("img[name='addImage']").length>=counts){
//            $.toast("只能上传" + counts + "张图片");
//            return;
//          }
            var that = this;
            model.appendImageList($(that)[0].files[0]);
          });
        }
        
        //拍照
        $("#main").on("click","#uploadFromCamera", function (e) {
          model.getImageFromCamera();
        });
        //从相册中选择
        $("#main").on("click","#uploadFromAlbums", function (e) {
          model.getFileImageAlbum();
        });
        
        //移除当前图片
        $("#main").on("click", "b[name='removeThisImage']", function(){
          console.log('remove image');
          $(this).parent().remove();
            if( 0==$("img[name='addImage']").length ){
            	$("span[name='selectUploadType']").show();
            }
        });
      },
      getFileImageAlbum:function(){
        var model = this;
        plus.gallery.pick(
          function (path) {
            $.showWaiting();
            model.appendImageList(path);
          },
          function (e) {
            console.log(e);
          },
          { filter: "image" }
        );
      },
      getImageFromCamera:function(){
        var model = this;
        var cmr = plus.camera.getCamera(1);
        if(null == cmr){
          $.toast("没有找到摄像头");
        }else{
            $.showWaiting();
          	cmr.captureImage(function (p) {
          	console.log( "getImageFromCamera" );
            plus.io.resolveLocalFileSystemURL(p,
            function (entry) {
              var path = "file://" + entry.fullPath;
              model.appendImageList(path);
            },
            function (e) {
              $.toast(e.message);
            });
          },
          function (e) { $.closeWaiting(); },
          { filename: "_doc/camera/" });                
        }
      },    
      appendImageList:function(path){
				console.log( JSON.stringify(path) );
				var lastIndex = path.lastIndexOf(".");
				var suffix = path.substring(lastIndex+1);
				if(['jpg','gif','png','bmp'].indexOf(suffix) == -1){
					$.toast("图片格式不对");
					return;
				}
        var model = this;
        html=[];
        lrz(path,
        {
          quality: 0.7,
          done: function (result) {
            html.push('<span class="tianjiaan">');
            html.push('<b name="removeThisImage" class="yguanbi"><i class="iconfont icon-diancha"></i></b>');
            html.push('<img name="addImage" style="width:70px;height:70px;" src="'+ result.base64 +'" />');
            html.push('</span>');
            $("span[name='selectUploadType']").before(html.join(''));
            if(model.maxImageCount && model.maxImageCount <=  $("img[name='addImage']").length){
            	$("span[name='selectUploadType']").hide();
            }
            if(AGENT_CLIENT.APP){
              model.hide();
            }
            $.closeWaiting();
          }
        });      
      },      
    }
});
